import React from "react";

// 引入组件
import Home from "@/pages/Home"

// 引入当前目录下面的所有文件 
const ModuleFiles = require.context('./', true, /.tsx$/)

// 创建路由容器
const RouterMap: any = [];

// 筛选不需要合并的文件
const NoNeedAuth = ['index', 'auth']

// 循环遍历文件
ModuleFiles.keys().map((item, index) => {
    // 获取文件名
    const routerName = item.replace(/^.\/(.*)\.tsx$/, "$1")
    // 判断是否不需要合并
    if (!NoNeedAuth.includes(routerName)) {
        // 读取文件里面的模块内容
        const ModuleFile = ModuleFiles(item)
        RouterMap.push(...ModuleFile.default)
    }
})


const RouterList = () => {
    // 获取cookie
    let business = React.Cookie.load("business")??{}
    let mobile = business.mobile??""
    let id = business.id??0

    //生成路由跳转对象,并直接覆盖到全局React自定义属性上
    const navigate = React.Router.useNavigate()
    React.navigate = navigate
    React.Business = business

    //构建一个路由守卫的子组件,用于判断登录情况
    const AuthLogin = (props:any) => {
        if(props.auth){
            // 存在auth，需要判断登录
            // 判断是business是否为空
            if(Object.getOwnPropertyNames(business).length <= 0){
                // 为空，未登录，去登录界面
                // 重定向跳转
                return (<React.Router.Navigate to="/business/login" />)
            }
            //发送请求
            let result:any = React.HTTP.post("/business/check",{mobile,id})
            result.then((result:any)=>{
                if(result.code == 1){
                    // 验证通过
                    //刷新覆盖一下cookie
                    React.Cookie.remove("business")
                    React.Cookie.save('business', result.data , { path: '/' })
                    return (props.element)
                }else{
                    // 清除cookie
                    React.Cookie.remove("business")
                    return (<React.Router.Navigate to="/business/login" />)
                }
            })
        }else{
            // 判断是否是登录界面
            // if(props.path == "login" && Object.getOwnPropertyNames(business).length > 0){
            //     return(<React.Router.Navigate to="/business/index" />)
            // }
            // 不需要判断登录，直接进入目标路由
            return (props.element)
        }
        return (props.element)
    }

    return (
        <React.Router.Routes>
            {/* 默认首页 */}
            <React.Router.Route path="/" element={<Home />}></React.Router.Route>
            {/* 其他页面 */}
            <React.Router.Route path="/" element={<><React.Router.Outlet /></>}>
                {
                    RouterMap.map((item: any, index: any) => {
                        return (
                            <React.Router.Route key={index} path={item.path} element={<item.element />} >
                                {item.children && item.children.map((son: any, key: any) => {
                                    return (<React.Router.Route key={key} path={son.path} element={<AuthLogin path={son.path ? son.path:""} auth={son.auth ? son.auth : false} element={<son.element />}/>}></React.Router.Route>)
                                })}
                            </React.Router.Route>
                        )
                    })
                }
            </React.Router.Route>
        </React.Router.Routes>
    )
}

export default RouterList
